package com.ytrsoft.task;

import com.ytrsoft.bean.Message;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public abstract class ExecutorHandler {

    private final ExecutorService executor = Executors.newSingleThreadExecutor();


    protected void start(Message msg) {
        executor.submit(() -> running(msg));
    }

    protected abstract void running(Message msg);


    protected void killNow() {
        executor.shutdown();
        try {
            if (!executor.awaitTermination(60, TimeUnit.SECONDS)) {
                executor.shutdownNow();
            }
        } catch (InterruptedException ie) {
            Thread.currentThread().interrupt();
        }
    }

}
